package com.gopay.bis.manager.bank.ebank;

import java.util.Map;

import javax.annotation.Resource;

import com.gopay.bis.common.util.BankLogUtils;
import com.gopay.bis.common.util.DateUtils;
import com.gopay.bis.common.util.TransUtils;
import com.gopay.common.constants.bank.BankConstants;
import com.gopay.common.constants.bank.CcbConstants;
import com.gopay.common.domain.bank.ccb.ebank.pay.CcbB2bMessage;
import com.gopay.common.exception.GopayException;
import com.gopay.remote.bank.businesses.BankService;

/**
 * 建行支付网关B2B对象封装
 * 
 * @CcbB2bManager.java
 * @author WangHongFei
 */
public class CcbB2bManager {
	
    @Resource(name = "ccbB2bBankServiceClient")
    private BankService ccbB2bBankServiceClient;
	
    @Resource(name = "ccbB2bBankServiceClientHna")
    private BankService ccbB2bBankServiceClientHna;
   
    @Resource(name = "ccbConfig")
    private Map<String, String> ccbConfig;
	
    /**
     * B2B签名
     * 
     * @param CcbB2bMessage
     * @return CcbB2bMessage
     */
    public CcbB2bMessage sign(CcbB2bMessage message) throws GopayException{
        message.setPostId(ccbConfig.get("CCB.B2B.PAYID"));
        message.setTxCode(ccbConfig.get("CCB.B2B.TXCODE.PAY.C"));
        message.setBankUrl(message.getPayUrl());
        message.setMerchantId(message.getMerID());
        message.setBranchId(ccbConfig.get("CCB.BRANCHID"));
        message.setCurCode(ccbConfig.get("CCB.CURCODE.RMB"));
        message.setPublicKey(message.getPublicKeyPath());
        message.setPlainMessage(buildPlain(message));
        BankLogUtils.printB2BManagerSignSendLog(message);
        String signStr;
        if(message.isHna()){
        	signStr = (String) ccbB2bBankServiceClientHna.sign(message);
        }else{
        	signStr = (String) ccbB2bBankServiceClient.sign(message);
        }
        message.setSignMessage(signStr);
        BankLogUtils.printB2BManagerSignReturnLog(message);
        return message;
    }    
    
    /**
     * B2B验签
     * 
     * @param CcbB2bMessage
     * @return boolean
     */
    public boolean verify(CcbB2bMessage message) throws GopayException{
        StringBuffer plain = new StringBuffer("");
        plain.append(message.getPostId()).append(message.getOrderId()).append(message.getCustId());
        plain.append(message.getAccNo()).append(message.getAccName()).append(message.getOrderAmt());
        plain.append(message.getStatus()).append(message.getRemark1()).append(message.getRemark2());
        plain.append(message.getTranFlag()).append(message.getTranTime()).append(message.getBranchName());
        message.setPlainMessage(plain.toString());
        BankLogUtils.printB2BManagerVerifySendLog(message);
        boolean flag;
        if(message.isHna()){
        	flag = (Boolean) ccbB2bBankServiceClientHna.verify(message);
        }else{
        	flag = (Boolean) ccbB2bBankServiceClient.verify(message);
        }
        BankLogUtils.printB2BManagerVerifyReturnLog(message, flag);
        return flag;
    }

    /**
     * B2B单笔订单查询
     * 
     * @param CcbB2bMessage
     * @return CcbB2bMessage
     */
    public CcbB2bMessage query(CcbB2bMessage message) throws GopayException{
        message.setLanguage("CN");// 语言
    	// 请求序列号
		message.setRequestSn(TransUtils.getPackageID());
		// 交易码
		message.setTxCode(CcbConstants.MERCHANT_PAY_QUERY);
		// 页次
		message.setPage("1");
		message.setPoscode((String) ccbConfig.get("CCB.B2B.PAYID"));// 外借pos机号
		message.setStatus("3"); // 订单流水状态(0:交易失败,1:交易成功,2:待银行确认(针对未结流水查询);3:全部)
		/* 按订单号查询时，时间段不起作用 */
		message.setStartDate(DateUtils.date2string(DateUtils.now(), "yyyyMMdd"));
	    message.setEndDate(DateUtils.date2string(DateUtils.now(), "yyyyMMdd"));
        BankLogUtils.printB2BManagerQuerySendLog(message, "");
        CcbB2bMessage ccbMessage;
        if(message.isHna()){
        	ccbMessage = (CcbB2bMessage) ccbB2bBankServiceClientHna.query(message);
        }else{
        	ccbMessage = (CcbB2bMessage) ccbB2bBankServiceClient.query(message);
        }
        ccbMessage.setOutStlmCd(BankConstants.BANK_LOGO_CCB); // 为了区分日志输出
        BankLogUtils.printB2BManagerQueryReturnLog(ccbMessage);
        return ccbMessage;
    }
    
    /**
     * 构建B2B明文串
     * 
     * @param CcbB2bMessage
     * @return String
     */
    private String buildPlain(CcbB2bMessage message) {
        StringBuffer sb = new StringBuffer("");
        sb.append("MERCHANTID=").append(message.getMerchantId());
        sb.append("&POSID=").append(message.getPostId());
        sb.append("&BRANCHID=").append(message.getBranchId());
        sb.append("&ORDERID=").append(message.getOrderId());
        sb.append("&PAYMENT=").append(message.getAmount().toString());
        sb.append("&CURCODE=").append(message.getCurCode());
        sb.append("&TXCODE=").append(message.getTxCode());
        sb.append("&REMARK1=").append("");
        sb.append("&REMARK2=").append("");
        return sb.toString();
    }
    
}
